package cn.iocoder.yudao.module.shop.service.memberhealth;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.memberhealth.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberhealth.ShopMemberHealthDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.memberhealth.ShopMemberHealthConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.memberhealth.ShopMemberHealthMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 收支记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ShopMemberHealthServiceImpl implements ShopMemberHealthService {

    @Resource
    private ShopMemberHealthMapper memberHealthMapper;

    @Override
    public Long createMemberHealth(ShopMemberHealthCreateReqVO createReqVO) {
        ShopMemberHealthDO memberHealthDO = getMemberHealthByUserId(createReqVO.getMemberId());
        if(memberHealthDO != null){
            throw exception(MEMBER_HEALTH_HAD_EXISTS);
        }

        // 插入
        ShopMemberHealthDO memberHealth = ShopMemberHealthConvert.INSTANCE.convert(createReqVO);
        memberHealthMapper.insert(memberHealth);
        // 返回
        return memberHealth.getId();
    }

    @Override
    public void updateMemberHealth(ShopMemberHealthUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberHealthExists(updateReqVO.getId());
        // 更新
        ShopMemberHealthDO updateObj = ShopMemberHealthConvert.INSTANCE.convert(updateReqVO);
        memberHealthMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberHealth(Long id) {
        // 校验存在
        validateMemberHealthExists(id);
        // 删除
        memberHealthMapper.deleteById(id);
    }

    private void validateMemberHealthExists(Long id) {
        if (memberHealthMapper.selectById(id) == null) {
            throw exception(MEMBER_HEALTH_NOT_EXISTS);
        }
    }

    @Override
    public ShopMemberHealthDO getMemberHealth(Long id) {
        return memberHealthMapper.selectById(id);
    }

    @Override
    public ShopMemberHealthDO getMemberHealthByUserId(Long userId){
        return memberHealthMapper.selectOne(ShopMemberHealthDO::getMemberId, userId);
    }

    @Override
    public List<ShopMemberHealthDO> getMemberHealthList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberHealthMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopMemberHealthDO> getMemberHealthPage(ShopMemberHealthPageReqVO pageReqVO) {
        return memberHealthMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopMemberHealthDO> getMemberHealthList(ShopMemberHealthExportReqVO exportReqVO) {
        return memberHealthMapper.selectList(exportReqVO);
    }

}
